package org.supda.system.controller.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;
import org.supda.system.entity.TResources;
import org.supda.system.exception.ParameterException;
import org.supda.system.exception.ServiceException;
import org.supda.system.service.ResourceService;
import org.supda.system.service.UserService;
import org.supda.system.util.PageUtil;

/**
 * 
 * @author supda
 * @Date 2016-12-29 下午2:24:58
 * 
 * 所有controller的基类，提供公共参数
 */
public class BaseController {
	
	protected final static String BASE_VIEW_PACKAGE = "WEB-INF/views/jsp/";
	
	protected final static String SYSTEM_MANAGEMENT_VIEW_PACKAGE = BASE_VIEW_PACKAGE+"system/";
	
	protected final static String SYSTEM_MONITOR_VIEW_PACKAGE = BASE_VIEW_PACKAGE+"monitor/";
	
	protected final static String RESPONSE_SUCCESS = "success";
	
	protected final static String RESPONSE_FAIL = "fail";
	
	
	//分页
	protected PageUtil page;
	
	@Inject
	private ResourceService resourceService;
	
	/**
	 * 取到当前会话session
	 * @return
	 */
	protected Session getSession(){
		
		Session session = SecurityUtils.getSubject().getSession();
		
		return session!=null?session:null;
	}
    
	/**
	 * 统一异常处理
	 * @param request
	 * @param response
	 * @param ex
	 * @return
	 */
    @ExceptionHandler  
    public  ModelAndView exceptionProcess(HttpServletRequest request, HttpServletResponse  
            response, RuntimeException ex) {  
    	
    	 Map<String, Object> model = new HashMap<String, Object>();
		 model.put("ex", ex);
          
          // 根据不同错误转向不同页面  
          if(ex instanceof ServiceException) {  
        	  response.setStatus(1001);//业务异常
        	  return new ModelAndView(BASE_VIEW_PACKAGE+"exception/error-system", model);
          }else if(ex instanceof ParameterException) {  
        	  response.setStatus(1002);//业务异常
        	  return new ModelAndView(BASE_VIEW_PACKAGE+"exception/error-parameter", model);
          } else {  
              return new ModelAndView(BASE_VIEW_PACKAGE+"exception/error", model);
          }  
    } 
	
	/**
	 * 得到操作Id
	 * @return
	 */
	protected Long getOID() {
		// 获取request
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		
		return Long.parseLong(request.getParameter("OID"));
	}
	/**
	 * 得到操作ID
	 * @return
	 */
	protected List<TResources> getOperatorRes(){
		return resourceService.findByParentId(getOID());	
	}

	public PageUtil getPage() {
		return page;
	}

	public void setPage(PageUtil page) {
		this.page = page;
	}
}
